package com.hello.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hello.common.log.annotation.Log;
import com.hello.common.log.enums.BusinessType;
import com.hello.system.annotation.ContentSecurity;
import com.hello.system.domain.TreeHoleEntries;
import com.hello.system.service.ITreeHoleEntriesService;
import com.hello.common.core.web.controller.BaseController;
import com.hello.common.core.web.domain.AjaxResult;
import com.hello.common.core.utils.poi.ExcelUtil;
import com.hello.common.core.web.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 树洞内容Controller
 * 
 * @author hello
 * @date 2025-05-15
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/entries")
public class TreeHoleEntriesController extends BaseController
{
    private final ITreeHoleEntriesService treeHoleEntriesService;

    /**
     * 查询树洞内容列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TreeHoleEntries treeHoleEntries)
    {
        startPage();
        List<TreeHoleEntries> list = treeHoleEntriesService.selectTreeHoleEntriesList(treeHoleEntries);
        return getDataTable(list);
    }

    /**
     * 导出树洞内容列表
     */
    @Log(title = "树洞内容", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TreeHoleEntries treeHoleEntries)
    {
        List<TreeHoleEntries> list = treeHoleEntriesService.selectTreeHoleEntriesList(treeHoleEntries);
        ExcelUtil<TreeHoleEntries> util = new ExcelUtil<TreeHoleEntries>(TreeHoleEntries.class);
        util.exportExcel(response, list, "树洞内容数据");
    }

    /**
     * 获取树洞内容详细信息
     */
    @GetMapping(value = "/{entryId}")
    public AjaxResult getInfo(@PathVariable("entryId") Long entryId)
    {
        return success(treeHoleEntriesService.selectTreeHoleEntriesByEntryId(entryId));
    }

    /**
     * 新增树洞内容
     */
    @Log(title = "树洞内容", businessType = BusinessType.INSERT)
    @PostMapping
    @ContentSecurity(fieldName = "content")
    public AjaxResult add(@RequestBody TreeHoleEntries treeHoleEntries)
    {
        // 内容安全检测由@ContentSecurity注解处理
        int rows = treeHoleEntriesService.insertTreeHoleEntries(treeHoleEntries);
        if (rows > 0) {
            TreeHoleEntries entry = treeHoleEntriesService.selectTreeHoleEntriesByEntryId(treeHoleEntries.getEntryId());
            AjaxResult result = AjaxResult.success("新增成功");
            result.put("data", entry);
            return result;
        }
        return error("新增树洞内容失败");
    }

    /**
     * 修改树洞内容
     */
    @Log(title = "树洞内容", businessType = BusinessType.UPDATE)
    @PutMapping
    @ContentSecurity(fieldName = "content")
    public AjaxResult edit(@RequestBody TreeHoleEntries treeHoleEntries)
    {
        // 内容安全检测由@ContentSecurity注解处理
        return toAjax(treeHoleEntriesService.updateTreeHoleEntries(treeHoleEntries));
    }

    /**
     * 删除树洞内容
     */
    @Log(title = "树洞内容", businessType = BusinessType.DELETE)
	@DeleteMapping("/{entryIds}")
    public AjaxResult remove(@PathVariable Long[] entryIds)
    {
        return toAjax(treeHoleEntriesService.deleteTreeHoleEntriesByEntryIds(entryIds));
    }
    
    /**
     * 给树洞内容点赞
     */
    @Log(title = "树洞点赞", businessType = BusinessType.UPDATE)
    @PostMapping("/like/{entryId}")
    public AjaxResult like(@PathVariable("entryId") Long entryId)
    {
        return toAjax(treeHoleEntriesService.likeTreeHoleEntry(entryId));
    }
    
    /**
     * 取消树洞内容点赞
     */
    @Log(title = "取消树洞点赞", businessType = BusinessType.UPDATE)
    @PostMapping("/unlike/{entryId}")
    public AjaxResult unlike(@PathVariable("entryId") Long entryId)
    {
        return toAjax(treeHoleEntriesService.unlikeTreeHoleEntry(entryId));
    }
    
    /**
     * 给树洞内容不喜欢
     */
    @Log(title = "树洞不喜欢", businessType = BusinessType.UPDATE)
    @PostMapping("/dislike/{entryId}")
    public AjaxResult dislike(@PathVariable("entryId") Long entryId)
    {
        return toAjax(treeHoleEntriesService.dislikeTreeHoleEntry(entryId));
    }
    
    /**
     * 取消树洞内容不喜欢
     */
    @Log(title = "取消树洞不喜欢", businessType = BusinessType.UPDATE)
    @PostMapping("/undislike/{entryId}")
    public AjaxResult undislike(@PathVariable("entryId") Long entryId)
    {
        return toAjax(treeHoleEntriesService.undislikeTreeHoleEntry(entryId));
    }
    
    /**
     * 增加树洞内容查看次数
     */
    @Log(title = "增加树洞查看次数", businessType = BusinessType.UPDATE)
    @PostMapping("/view/{entryId}")
    public AjaxResult incrementViews(@PathVariable("entryId") Long entryId)
    {
        // 先检查树洞条目是否存在
        TreeHoleEntries entry = treeHoleEntriesService.selectTreeHoleEntriesByEntryId(entryId);
        if (entry == null) {
            return error("树洞内容不存在");
        }
        
        // 增加查看次数
        int rows = treeHoleEntriesService.incrementViewsCount(entryId);
        
        return rows > 0 ? success("增加查看次数成功") : error("增加查看次数失败");
    }
}
